package com.yq.testmodule.vp2;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * 事件拦截的ScrollView 优先处理自己滚动
 */
public class EventInterceptScrollView extends ScrollView {
    public EventInterceptScrollView(Context context) {
        super(context);
        init();
    }

    public EventInterceptScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EventInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public EventInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    protected int mTouchSlop;

    private void init() {
        ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
    }


    float mDownY, mDownX;

    boolean isDragScrolling = false;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            mDownY = ev.getY();
            mDownX = ev.getX();
            isDragScrolling = false;
            getParent().requestDisallowInterceptTouchEvent(true);
        } else if (action == MotionEvent.ACTION_MOVE) {
            float y = ev.getY();
            float x = ev.getX();
            float dy = y - mDownY;
            float dx = x - mDownX;

            float absDy = Math.abs(dy);
            float absDx = Math.abs(dx);

            if (absDy >= mTouchSlop || absDx>=mTouchSlop ) {
                if(absDy < absDx){//横向滑动，不拦截
//                    Log.e("YYYY", "横向滑动，不拦截");
                    getParent().requestDisallowInterceptTouchEvent(false);
                    return super.dispatchTouchEvent(ev);
                }else {
                    if (dy < 0) {
                        //到底了(如果是手势滚动过程中
                        if (isScrollToBottom()&&!isDragScrolling) {
                            getParent().requestDisallowInterceptTouchEvent(false);
//                            Log.e("YYYY", "到底了 父控件拦截");
                        } else {
                            isDragScrolling = true;
                        }
                    } else {
                        //到头了
                        if (isScrollToTop()&&!isDragScrolling) {
                            getParent().requestDisallowInterceptTouchEvent(false);
//                            Log.e("YYYY", "到头了 父控件拦截");
                        } else {
                            isDragScrolling = true;
                        }
                    }
                }
            }
        }

        return super.dispatchTouchEvent(ev);
    }


    //判断Scrollview是否滑动到底部
    public boolean isScrollToBottom() {
        return getScrollY() + getHeight() >= computeVerticalScrollRange();
    }

    //判断Scrollview是否滑动到顶部
    public boolean isScrollToTop() {
        return getScrollY() == 0;
    }

}
